from django.shortcuts import render
from django.views.generic import TemplateView
from commodity.models import Types, CommodityInfos


# Create your views here.
def indexView(request):

    title="首页"
    classContent = ""#用于页面样式控制，为active
    #销量最高的八个商品,显示在首页中轮播
    commodityInfo = CommodityInfos.objects.order_by('-sold').all()[:8]
    #获取所有商品分类
    types = Types.objects.all()

    #所有儿童服饰
    c1=[x.seconds for x in types if x.firsts == '儿童服饰']
    #选出儿童服饰的前五个放在一楼展示页
    clothes = CommodityInfos.objects.filter(types__in = c1).order_by('-sold')[:5]

    #选出宝宝辅食放二楼
    f1=[x.seconds for x in types if x.firsts =='宝宝辅食']
    food = CommodityInfos.objects.filter(types__in = f1).order_by('-sold')[:5]

    #三楼商品
    g1=[x.seconds for x in types if x.firsts == '儿童用品']
    goods = CommodityInfos.objects.filter(types__in = g1).order_by('-sold')[:5]


    return render(request,'index.html',locals())

class indexClassView(TemplateView):
    #制定渲染的模版
    template_name='index.html'
    template_engine=None
    #指定响应的type
    content_type=None
    #额外的模版上下文变量
    ertra_context= {'title':'首页','classContent':''}


    #重写获取上下文数据的方法
    def get_context_data(self,**kwargs):
        context=super(indexClassView,self).get_context_data(**kwargs)
        context['commodityInfos']=CommodityInfos.objects.order_by('-sold').all()[:8]
        types=Types.objects.all()

        cl = [x.seconds for x in types if x.firsts == '儿童服饰']
        # 选出儿童服饰的前五个放在一楼展示页
        context['clothes'] = CommodityInfos.objects.filter(types__in=cl).order_by('-sold')[:5]

        # 选出宝宝辅食放二楼
        fl = [x.seconds for x in types if x.firsts == '宝宝辅食']
        context['foods'] = CommodityInfos.objects.filter(types__in=fl).order_by('-sold')[:5]

        # 三楼商品
        gl = [x.seconds for x in types if x.firsts == '儿童用品']
        context['goods'] = CommodityInfos.objects.filter(types__in=gl).order_by('-sold')[:5]
        return context

    #处理http的GET请求
    def get(self, request, *args, **kwargs):
        #获取上下文数据
        context=self.get_context_data(**kwargs)
        #渲染模版并返回
        return self.render_to_response(context)
    #处理POST请求
    def post(self, request, *args, **kwargs):
        context=self.get_context_data(**kwargs)
        return self.render_to_response(context)

